﻿Namespace PowerpointOperations
    Public Class PowerPointTranslator
        Private Const AutomationCopyrightValue = "Automation provided by PPTTranslator for L3D v2.x, (C)2011-2015 Vasco Mantellini (v2.0.1)"
        Private Const LayoutDateValue = #12/6/2011#
        Private Const LayoutVersionValue = "1.0.1"
        Private Const L3DPPOFontFamily = "Arial"
        Private Const L3DPPOPlanYearOffset = 1
        Private Const L3DPPOTopPiano = 129
        Private Const L3DPPOLeftPiano = 57
        Private Const L3DPPOAltezzaPiano = 381
        Private Const L3DPPOLarghezzaMese = 14
        Private Const L3DPPOLarghezzaAnno = L3DPPOLarghezzaMese * 12
        Private Const L3DPPOAltezzaMese = 14
        Private Const L3DPPOAltezzaAnno = 19
        Private Const L3DPPOLarghezzaMilestone = 12
        Private Const L3DPPOAltezzaMilestone = 16
        Private Const L3DPPOAltezzaBarre = 16
        Private Const L3DPPOAltezzaBarreConsuntivo = 8
        Private Const L3DPPOOccupazioneVerticaleMilestone = 36
        Private Const L3DPPODistanzaVerticaleElementi = 45
        Public Event ProjectExported()

        Public ReadOnly Property AutomationCopyright
            Get
                Return AutomationCopyrightValue
            End Get
        End Property
        Public ReadOnly Property LayoutDate As Date
            Get
                Return LayoutDateValue
            End Get
        End Property
        Public ReadOnly Property LayoutVersion As String
            Get
                Return LayoutVersionValue
            End Get
        End Property
        Public Sub BuildPowerPointPresentation(ByVal PresentationObject As L3DObjectsLibrary.L3DPlatformPlan, ByVal SavePath As String)
            Dim PowerPoint = New Microsoft.Office.Interop.PowerPoint.Application
            Dim Presentation As Microsoft.Office.Interop.PowerPoint.Presentation
            If Single.Parse(PowerPoint.Version) >= 12 Then
                Presentation = PowerPoint.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse)
            Else
                Presentation = PowerPoint.Presentations.Add
            End If
                Presentation.PageSetup.SlideSize = Microsoft.Office.Interop.PowerPoint.PpSlideSizeType.ppSlideSizeA4Paper
                BuildSlideLayout(Presentation, PresentationObject)
                Dim pLastslide As Microsoft.Office.Interop.PowerPoint.Slide
            For Each Project As L3DObjectsLibrary.Project In PresentationObject.Projects
                pLastslide = Presentation.Slides.Add(Presentation.Slides.Count + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank)
                ProjectYearsInsert(pLastslide, Project.FirstProjectYear(L3DPPOPlanYearOffset))
                StatusLineInsert(pLastslide, Project, PresentationObject.PresentationStatusDate)
                ProjectInfoInsert(pLastslide, Project)
                ProjectSynthesisInsert(pLastslide, PresentationObject.PresentationStatusDate) 'non è inserito nello schema per permetterne lo spostamento manuale sulla presentazione
                Dim pTop = L3DPPOTopPiano + L3DPPOAltezzaAnno + L3DPPOAltezzaMese + 10
                Dim L3DPPOltimoTop = L3DPPOTopPiano + L3DPPOAltezzaAnno + L3DPPOAltezzaMese + 5
                Dim pInizioProgetto = CDate("01/01/" & Str(Project.FirstProjectYear(L3DPPOPlanYearOffset)))
                For Each Elemento As L3DObjectsLibrary.ProjectElement In Project.ProjectElements
                    If Elemento.ElementType = L3DObjectsLibrary.ElementTypeValue.L3DMilestone Then
                        MilestoneInsert(pLastslide, Elemento.Milestones.First, CDate("01/01/" & Project.FirstProjectYear(L3DPPOPlanYearOffset)), pTop)
                    ElseIf Elemento.ElementType = L3DObjectsLibrary.ElementTypeValue.L3DPhase Then
                        PhaseInsert(pLastslide, Elemento, CDate("01/01/" & Project.FirstProjectYear(L3DPPOPlanYearOffset)), pTop)
                    End If
                Next
                RaiseEvent ProjectExported()
            Next
            PowerPoint.Presentations(1).SaveAs(SavePath)
            PowerPoint.Presentations(1).Close()
            PowerPoint.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(PowerPoint)
            pLastslide = Nothing
            PowerPoint = Nothing
        End Sub
        Private Sub BuildSlideLayout(ByVal Presentation As Microsoft.Office.Interop.PowerPoint.Presentation, ByVal PresentationObject As L3DObjectsLibrary.L3DPlatformPlan)
            Dim pLeft As Integer
            Dim pLeft2 As Integer
            Dim pMesi(11) As String
            pMesi = {"J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"}
            With Presentation.SlideMaster
                .Shapes.Range.Delete()
                AddHeaderStdRectangle(Presentation, 36, 21, 170, 100, Chr(13) & Chr(13) & "Program Name:" & Chr(13) & Chr(13) & Chr(13) & Chr(13) & Format(PresentationObject.PresentationStatusDate, "dd/MM/yy"), 10, True)
                Dim pPercorsoLogo = My.Application.Info.DirectoryPath & "\~l~o~g~o~.bmp.tmp"
                Dim pLogo As System.Drawing.Image = My.Resources.Logo
                pLogo.Save(pPercorsoLogo)
                .Shapes.AddPicture(pPercorsoLogo, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 95, 25)
                System.IO.File.Delete(pPercorsoLogo)
                AddHeaderStdRectangle(Presentation, 213, 21, 445, 100, "LEVEL III - PD - In-Vehicle Validation Plan/Status", 16, True)
                AddHeaderStdRectangle(Presentation, 665, 21, 85, 100, "Evaluation", 10, True)
                pLeft = L3DPPOLeftPiano
                For i = 1 To 4
                    pLeft2 = pLeft
                    With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft, L3DPPOTopPiano, L3DPPOLarghezzaMese * 12, L3DPPOAltezzaPiano)
                        .Fill.ForeColor.RGB = RGB(255, 255, 255)
                        .Line.Weight = 1
                        .Line.ForeColor.RGB = RGB(0, 0, 0)
                    End With
                    For j = 1 To 12
                        With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft2, 148, L3DPPOLarghezzaMese, L3DPPOAltezzaMese)
                            .Fill.ForeColor.RGB = RGB(255, 255, 255)
                            .Line.Weight = 1
                            .Line.ForeColor.RGB = RGB(0, 0, 0)
                            With .TextFrame.TextRange
                                .Text = pMesi(j - 1)
                                .Font.Size = 8
                                .Font.Bold = False
                                .Font.Name = L3DPPOFontFamily
                                .Font.Color.RGB = RGB(0, 0, 0)
                            End With
                        End With
                        pLeft2 = pLeft2 + L3DPPOLarghezzaMese
                    Next j
                    pLeft = pLeft2
                Next i
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 67, 460, 30, 10)
                    .Fill.ForeColor.RGB = RGB(255, 255, 255)
                    .Line.Weight = 1
                    .Line.ForeColor.RGB = RGB(0, 0, 0)
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 100, 460, 100, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "ORIGINAL PLAN"
                        .Characters.Font.Size = 10
                        .Characters.Font.Bold = True
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        .Characters.Font.Name = L3DPPOFontFamily
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 67, 475, 30, 10)
                    .Fill.ForeColor.RGB = RGB(0, 0, 0)
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 100, 475, 100, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "COMPLETED"
                        .Characters.Font.Size = 10
                        .Characters.Font.Bold = True
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        .Characters.Font.Name = L3DPPOFontFamily
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 67, 490, 30, 10)
                    .Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 4)
                    .Fill.ForeColor.RGB = RGB(255, 153, 0)
                    .Fill.BackColor.RGB = RGB(255, 255, 255)
                    .Line.Weight = 1
                    .Line.ForeColor.RGB = RGB(0, 0, 0)
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 100, 490, 100, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "CURRENT PLAN"
                        .Characters.Font.Size = 10
                        .Characters.Font.Bold = True
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        .Characters.Font.Name = L3DPPOFontFamily
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 60, 420, 200, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "RED - TEST STOPPED 0 - 50%" & Chr(13)
                        .Text = .Text & "YELLOW - TEST STOPPED 51 - 99%" & Chr(13)
                        .Text = .Text & "GREEN - TEST COMPLETED - 100%" & Chr(13)
                        .Text = .Text & "BLACK - RUNNING OR AWAITING SOT" & Chr(13)
                        .Text = .Text & "BLUE - DELETED BY FIAT FOR COST REDUCTION"
                        .Paragraphs(1, 1).Font.Color.RGB = RGB(255, 0, 0)
                        .Paragraphs(2, 1).Font.Color.RGB = RGB(204, 153, 0)
                        .Paragraphs(3, 1).Font.Color.RGB = RGB(0, 204, 102)
                        .Paragraphs(4, 1).Font.Color.RGB = RGB(0, 0, 0)
                        .Paragraphs(5, 1).Font.Color.RGB = RGB(0, 0, 255)
                        .Characters.Font.Size = 8
                        .Characters.Font.Bold = True
                        .Characters.Font.Name = L3DPPOFontFamily
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 50, 520, 10, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    .TextFrame.WordWrap = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "Layout revision " & LayoutVersion & " of " & Format(LayoutDate, "dd MMMM yyyy") & ". " & AutomationCopyright
                        .Characters.Font.Size = 8
                        .Characters.Font.Name = L3DPPOFontFamily
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                With .Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 560, 520, 10, 10)
                    .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                    .TextFrame.WordWrap = Microsoft.Office.Core.MsoTriState.msoFalse
                    With .TextFrame.TextRange
                        .Text = "Information owner: " & PresentationObject.PresentationOwner
                        .Characters.Font.Size = 10
                        .Characters.Font.Name = L3DPPOFontFamily
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                    End With
                    .Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
            End With
        End Sub
        Private Sub ProjectYearsInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal FirstYear As Integer)
            Dim pAnni(3) As String
            Dim pLeft = L3DPPOLeftPiano
            pAnni(0) = Str$(FirstYear - 1)
            For i = 0 To 3
                pAnni(i) = Str$(FirstYear + i)
            Next i
            For i = 0 To 3
                With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft, L3DPPOTopPiano, L3DPPOLarghezzaAnno, L3DPPOAltezzaAnno)
                    .Fill.ForeColor.RGB = RGB(255, 255, 255)
                    .Line.Weight = 1
                    .Line.ForeColor.RGB = RGB(0, 0, 0)
                    With .TextFrame.TextRange
                        .Text = pAnni(i)
                        .Characters.Font.Bold = True
                        .Characters.Font.Size = 16
                        .Characters.Font.Name = L3DPPOFontFamily
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                    End With
                End With
                pLeft = pLeft + L3DPPOLarghezzaAnno
            Next i
        End Sub
        Private Sub StatusLineInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal Project As L3DObjectsLibrary.Project, ByVal StatusDate As Date)
            Dim StartProjectDay As Date = CDate("01/01/" & Project.FirstProjectYear(L3DPPOPlanYearOffset))
            Dim pPosizione As Integer = L3DPPOLeftPiano + Val(DateDiff(DateInterval.Day, StartProjectDay, StatusDate)) / (365 * 4) * (L3DPPOLarghezzaMese * 48)
            With TargetSlide.Shapes.AddLine(pPosizione, L3DPPOTopPiano + L3DPPOAltezzaAnno + L3DPPOAltezzaMese, pPosizione, L3DPPOTopPiano + L3DPPOAltezzaPiano)
                .Line.ForeColor.RGB = RGB(255, 0, 0)
                .Line.Weight = 1.5
            End With
        End Sub
        Private Sub ProjectInfoInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal Project As L3DObjectsLibrary.Project)
            'Qui inserisco il nome del progetto
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 36, 21, 170, 100)
                .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                .Line.Visible = False
                With .TextFrame.TextRange
                    .Text = Chr(13) & Project.Name
                    .Characters.Font.Bold = True
                    .Characters.Font.Size = 10
                    .Characters.Font.Name = L3DPPOFontFamily
                    .Characters.Font.Color.RGB = RGB(0, 0, 0)
                End With
            End With
            'Qui inserisco gli highlights del progetto
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 213, 21, 445, 100)
                .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
                .Line.Visible = False
                With .TextFrame
                    With .TextRange
                        .Text = Chr(13) & Chr(13) & "Highlights: " & Project.ProjectHighlights
                        .Characters.Font.Bold = True
                        .Characters.Font.Size = 12
                        .Characters.Font.Name = L3DPPOFontFamily
                        .Characters.Font.Color.RGB = RGB(0, 0, 0)
                        With .ParagraphFormat
                            .Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                        End With
                    End With
                    .VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorTop
                    .WordWrap = True
                End With
            End With
            'Qui inserisco la classificazione di rischio
            Select Case Project.ProjectRisk
                Case L3DObjectsLibrary.ProjectConstantValues.ProjectStatusValues.L3DStatusGreen
                    With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeOval, 693, 56, 30, 30)
                        .Fill.ForeColor.RGB = RGB(0, 168, 0)
                        .Line.Weight = 2
                        .Line.ForeColor.RGB = RGB(0, 0, 0)
                    End With
                Case L3DObjectsLibrary.ProjectConstantValues.ProjectStatusValues.L3DStatusYellow
                    With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeIsoscelesTriangle, 693, 56, 30, 30)
                        .Fill.ForeColor.RGB = RGB(250, 253, 0)
                        .Line.Weight = 2
                        .Line.ForeColor.RGB = RGB(0, 0, 0)
                    End With
                Case L3DObjectsLibrary.ProjectConstantValues.ProjectStatusValues.L3DStatusRed
                    With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeCross, 693, 56, 30, 30)
                        .Fill.ForeColor.RGB = RGB(255, 0, 0)
                        .Line.Weight = 2
                        .Line.ForeColor.RGB = RGB(0, 0, 0)
                        .Rotation = 45
                        .Adjustments(1) = 0.4
                    End With
            End Select
        End Sub
        Private Sub ProjectSynthesisInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal StatusDate As Date)
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, L3DPPOLeftPiano + 5, 300, L3DPPOLarghezzaAnno - 5, 110)
                .Name = "RiassuntoAccumuli"
                .Fill.ForeColor.RGB = RGB(255, 255, 255)
                .Line.Weight = 1
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                .TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorTop
                With .TextFrame.TextRange
                    .Text = "STATUS ACCUMULI AL " & Format(StatusDate, "dd/MM/yy") & Chr(13)
                    .Paragraphs(1, 1).Font.Color.RGB = RGB(255, 0, 0)
                    .Paragraphs(1, 1).Font.Bold = True
                    .Paragraphs(1, 1).Font.Name = L3DPPOFontFamily
                    .Paragraphs(1, 1).Font.Size = 10
                    .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                End With
            End With
        End Sub
        Private Sub PhaseInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal Elemento As L3DObjectsLibrary.ProjectElement, ByVal ProjectStart As Date, ByRef VerticalPosition As Integer)
            Dim pPercentualeEOBD As Integer
            Dim pKmPercorsi As Long
            Dim pKmPercorsiEOBD As Long
            Dim pKmDaPercorrereEOBD As Long
            Dim pShape As Microsoft.Office.Interop.PowerPoint.Shape
            pShape = TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 1, VerticalPosition, 100, 10)
            With pShape
                .Fill.Visible = False
                .Line.Visible = False
                With .TextFrame.TextRange
                    .Text = Elemento.Name
                    .Font.Size = 14
                    .Font.Name = L3DPPOFontFamily
                    .Font.Bold = True
                    .Font.Color.RGB = RGB(0, 0, 0)
                End With
                AutoSizeBox(pShape)
                .Left = L3DPPOLeftPiano + DateDiff(DateInterval.Day, ProjectStart, Elemento.ElementStartDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48) - .Width - 1
                .Top = .Top + ((L3DPPOAltezzaBarre + 4) * (Elemento.Groups.Count + 1)) / 2 - .Height / 2
            End With
            For Each Gruppo As L3DObjectsLibrary.Group In Elemento.Groups
                GroupInsert(TargetSlide, Gruppo, VerticalPosition, ProjectStart)
                VerticalPosition = VerticalPosition + L3DPPODistanzaVerticaleElementi * 0.6
                pKmDaPercorrereEOBD = Gruppo.AllCarsKm(True, False)
                pKmPercorsiEOBD = Gruppo.TotalGroupKmStatus(True)
                pKmPercorsi = Gruppo.TotalGroupKmStatus(False)
            Next
            If pKmPercorsi > 0 Then
                pShape = TargetSlide.Shapes("RiassuntoAccumuli")
                With pShape.TextFrame
                    With .TextRange
                        .Text = .Text & Chr(13) & "TOT " & UCase(Elemento.Name) & ": " & Format(pKmPercorsi, "###,###,###") & "km" & Chr(13)
                        If pKmDaPercorrereEOBD > 0 Then
                            pPercentualeEOBD = pKmPercorsiEOBD / pKmDaPercorrereEOBD * 100
                            .Text = .Text & "FLEET EOBD " & pPercentualeEOBD & "%" & Chr(13)
                            If Elemento.Remark <> "" Then
                                .Text = .Text & Elemento.Remark & Chr(13)
                            End If
                        End If
                        With .Paragraphs(3, .Paragraphs.Count - 2).Font
                            .Size = 7
                            .Bold = True
                            .Name = L3DPPOFontFamily
                            .Color.RGB = RGB(0, 0, 0)
                        End With
                    End With
                End With
                AutoSizeBox(pShape)
            End If
        End Sub
        Private Sub GroupInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal Gruppo As L3DObjectsLibrary.Group, ByVal VerticalPosition As Integer, ByVal ProjectStart As Date)
            Dim pConsuntivoKm As Integer
            Dim pKmDaPercorrere As Integer
            Dim pKmTotali As Integer
            Dim pLeft As Integer
            Dim pWidth As Integer
            Dim pXInizio As Integer
            Dim pYInizio As Integer
            Dim pXFine As Integer
            Dim pYFine As Integer

            For Each Car As L3DObjectsLibrary.Car In Gruppo.Cars
                pConsuntivoKm = pConsuntivoKm + Car.kmStatus
                pKmDaPercorrere = pKmDaPercorrere + Car.kmCity + Car.kmHS + Car.kmRG
            Next
            pLeft = L3DPPOLeftPiano + DateDiff(DateInterval.Day, ProjectStart, Gruppo.GroupStartDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48)          'posizione normalizzata su un piano largo 4 anni (48 mesi)
            pWidth = DateDiff(DateInterval.Day, Gruppo.GroupStartDate, Gruppo.GroupEndDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48)
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft, VerticalPosition, pWidth, L3DPPOAltezzaBarre)
                .Fill.ForeColor.RGB = RGB(255, 255, 255)
                .Line.Weight = 1
                .Line.ForeColor.RGB = RGB(0, 0, 0)
            End With
            If Gruppo.GroupStartDate <> Gruppo.GroupRealStartDate Or Gruppo.GroupEndDate <> Gruppo.GroupRealEndDate Then
                VerticalPosition = VerticalPosition + 5
            End If
            pLeft = L3DPPOLeftPiano + DateDiff(DateInterval.Day, ProjectStart, Gruppo.GroupRealStartDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48)          'posizione normalizzata su un piano largo 4 anni (48 mesi)
            pWidth = DateDiff(DateInterval.Day, Gruppo.GroupRealStartDate, Gruppo.GroupRealEndDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48)
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft, VerticalPosition, pWidth, L3DPPOAltezzaBarre)
                .Fill.TwoColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 4)
                .Fill.ForeColor.RGB = RGB(255, 153, 0)
                .Fill.BackColor.RGB = RGB(255, 255, 255)
                .Line.Weight = 1
                .Line.ForeColor.RGB = RGB(0, 0, 0)
            End With
            Dim pShape As Microsoft.Office.Interop.PowerPoint.Shape = TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 0, 0, 10, 10)
            With pShape
                .Fill.ForeColor.RGB = RGB(255, 255, 255)
                .Line.Weight = 1
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                With .TextFrame
                    .TextRange.Font.Color.RGB = RGB(0, 0, 0)
                    For Each Car As L3DObjectsLibrary.Car In Gruppo.Cars
                        pKmTotali = Car.kmCity + Car.kmHS + Car.kmRG
                        With .TextRange
                            .Text = .Text & Car.Name & " " & pKmTotali / 1000 & "kkm "
                            If Car.EOBD = True Then
                                .Text = .Text & "EOBD "
                            End If
                            .Text = .Text & "("
                            If Car.kmHS > 0 Then
                                .Text = .Text & Car.kmHS / 1000 & "kkm HS "
                                If Car.kmRG > 0 Then
                                    .Text = .Text & "+ "
                                End If
                            End If
                            If Car.kmRG > 0 Then
                                .Text = .Text & Car.kmRG / 1000 & "kkm RG"
                                If Car.kmCity > 0 Then
                                    .Text = .Text & "+ "
                                End If
                            End If
                            If Car.kmCity > 0 Then
                                .Text = .Text & Car.kmCity / 1000 & "kkm City"
                            End If
                            .Text = .Text & ")   " & Format(Car.kmStatus, "###,###,###") & "km"
                            If Car IsNot Gruppo.Cars(Gruppo.Cars.Count - 1) Then
                                .Text = .Text & Chr(13)
                            End If
                            Select Case LCase(Car.CarStatus)
                                Case "stop"
                                    If Car.kmStatus / pKmTotali < 0.5 Then
                                        .Paragraphs(.Paragraphs.Count, 1).Font.Color.RGB = RGB(255, 0, 0)
                                    Else
                                        .Paragraphs(.Paragraphs.Count, 1).Font.Color.RGB = RGB(204, 153, 0)
                                    End If
                                Case "cancel"
                                    .Paragraphs(.Paragraphs.Count, 1).Font.Color.RGB = RGB(0, 0, 255)
                                Case "completed"
                                    .Paragraphs(.Paragraphs.Count, 1).Font.Color.RGB = RGB(0, 204, 102)
                            End Select
                            .ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft
                            With .Font
                                .Size = 8
                                .Name = L3DPPOFontFamily
                                .Bold = True
                            End With
                        End With
                    Next
                End With
                AutoSizeBox(pShape)
                .Left = L3DPPOLeftPiano + 48 * L3DPPOLarghezzaMese - .Width - 5
                .Top = VerticalPosition - .Height - 10
                pXInizio = .Left
                If .Left < (pLeft + pWidth) + 10 Then
                    If .Top + .Height / 2 <= VerticalPosition + L3DPPOAltezzaBarre / 2 Then
                        pYInizio = .Top + .Height
                    Else
                        pYInizio = .Top
                    End If
                Else
                    pYInizio = .Top + .Height / 2
                End If
                If .Left < (pLeft + pWidth) + 10 Then
                    If .Top + .Height / 2 <= VerticalPosition + L3DPPOAltezzaBarre / 2 Then
                        pYInizio = .Top + .Height
                    Else
                        pYInizio = .Top
                    End If
                Else
                    pYInizio = .Top + .Height / 2
                End If
                If .Top + .Height / 2 < VerticalPosition - 10 Then
                    If .Top + .Height / 2 <= VerticalPosition + L3DPPOAltezzaBarre / 2 Then
                        pYFine = VerticalPosition
                    Else
                        pYFine = VerticalPosition + L3DPPOAltezzaBarre
                    End If
                Else
                    pYFine = VerticalPosition + L3DPPOAltezzaBarre / 2
                End If
            End With
            pXFine = pLeft + pWidth
            With TargetSlide.Shapes.AddLine(pXInizio, pYInizio, pXFine, pYFine)
                .Line.EndArrowheadLength = 3
                .Line.EndArrowheadWidth = 2
                .Line.EndArrowheadStyle = Microsoft.Office.Core.MsoArrowheadStyle.msoArrowheadStealth
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                .Line.Weight = 1
            End With
            If pKmDaPercorrere <= 0 Then
                pWidth = 0
            Else
                pWidth = pWidth * pConsuntivoKm / pKmDaPercorrere
            End If

            VerticalPosition = VerticalPosition + (L3DPPOAltezzaBarre - L3DPPOAltezzaBarreConsuntivo) / 2
            With TargetSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pLeft, VerticalPosition, pWidth, L3DPPOAltezzaBarreConsuntivo)
                .Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Line.Visible = False
            End With



        End Sub
        Private Sub MilestoneInsert(ByVal TargetSlide As Microsoft.Office.Interop.PowerPoint.Slide, ByVal Milestone As L3DObjectsLibrary.Milestone, ByVal ProjectStart As Date, ByRef VerticalPosition As Integer)
            If Milestone.ShowInPlan = False Then
                Exit Sub
            End If
            Dim pPosizione As Single
            pPosizione = L3DPPOLeftPiano + DateDiff(DateInterval.Day, ProjectStart, Milestone.MilestoneDate) / (365 * 4) * (L3DPPOLarghezzaMese * 48) 'posizione normalizzata su un piano largo 4 anni (48 mesi)
            With TargetSlide.Shapes
                With .AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeDiamond, pPosizione - (L3DPPOLarghezzaMilestone / 2), VerticalPosition, L3DPPOLarghezzaMilestone, L3DPPOAltezzaMilestone)
                    .Line.Weight = 1
                    .Line.ForeColor.RGB = RGB(0, 0, 0)
                    Select Case UCase(Milestone.Name)
                        Case "PROTO"
                            .Fill.ForeColor.RGB = RGB(0, 174, 0)
                        Case "VP"
                            .Fill.ForeColor.RGB = RGB(0, 0, 255)
                        Case "PS"
                            .Fill.ForeColor.RGB = RGB(255, 153, 0)
                        Case "SOP"
                            .Fill.ForeColor.RGB = RGB(255, 0, 0)
                    End Select
                End With
                With .AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pPosizione - 15, VerticalPosition + L3DPPOAltezzaMilestone + 2, 30, 20)
                    .Line.Visible = False
                    .Fill.ForeColor.RGB = RGB(255, 255, 255)
                    .TextFrame.TextRange.Text = Milestone.Name & Chr(13) & Format(Milestone.MilestoneDate, "dd MM yy")
                    .TextFrame.TextRange.Paragraphs(1, 1).Font.Size = 14
                    .TextFrame.TextRange.Paragraphs(2, 1).Font.Size = 8
                    .TextFrame.TextRange.Font.Bold = True
                    .TextFrame.TextRange.Font.Name = L3DPPOFontFamily
                    .TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
                    .TextFrame.WordWrap = Microsoft.Office.Core.MsoTriState.msoFalse
                End With
                If Milestone.Remark <> "" Then
                    With .AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, pPosizione - 15, VerticalPosition + L3DPPOAltezzaMilestone + 2, 30, 20)
                        .Fill.ForeColor.RGB = RGB(255, 255, 255)
                        .Line.Weight = 1
                        .Line.ForeColor.RGB = RGB(0, 0, 0)
                        .TextFrame.AutoSize = Microsoft.Office.Interop.PowerPoint.PpAutoSize.ppAutoSizeShapeToFitText
                        .TextFrame.TextRange.Text = Milestone.Remark
                        .TextFrame.TextRange.Font.Size = 8
                        .TextFrame.TextRange.Font.Bold = False
                        .TextFrame.TextRange.Font.Name = L3DPPOFontFamily
                        .TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
                        .TextFrame.WordWrap = Microsoft.Office.Core.MsoTriState.msoFalse
                        .Left = pPosizione - .Width - 40
                    End With
                End If
                If ((UCase(Milestone.Name) = "SOP" Or UCase(Milestone.Name) = "JOB1" Or Milestone.SpecialID = L3DObjectsLibrary.Milestone.SpecialIDValues.SOP)) Then
                    With .AddLine(pPosizione, L3DPPOTopPiano + L3DPPOAltezzaAnno + L3DPPOAltezzaMese, pPosizione, L3DPPOTopPiano + L3DPPOAltezzaPiano)
                        .Line.ForeColor.RGB = RGB(255, 0, 0)
                        .Line.Weight = 1.5
                        .Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineRoundDot
                    End With
                End If
            End With
            VerticalPosition = VerticalPosition + L3DPPODistanzaVerticaleElementi
        End Sub
        Private Sub AutoSizeBox(ByVal Shape As Microsoft.Office.Interop.PowerPoint.Shape)
            With Shape
                .TextFrame.WordWrap = False
                .Width = .TextFrame.TextRange.BoundWidth + .TextFrame.MarginLeft + .TextFrame.MarginRight
                .Height = .TextFrame.TextRange.BoundHeight + .TextFrame.MarginTop + .TextFrame.MarginBottom
            End With
        End Sub
        Private Sub AddHeaderStdRectangle(ByVal Presentation As Microsoft.Office.Interop.PowerPoint.Presentation, ByVal Left As Integer, ByVal Top As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal Text As String, ByVal TextHeight As Integer, ByVal Textbold As Boolean)
            With Presentation.SlideMaster.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRoundedRectangle, Left, Top, Width, Height)
                .Fill.ForeColor.RGB = RGB(255, 255, 255)
                .TextFrame.HorizontalAnchor = Microsoft.Office.Core.MsoHorizontalAnchor.msoAnchorNone
                .TextFrame.VerticalAnchor = Microsoft.Office.Core.MsoVerticalAnchor.msoAnchorTop
                .Line.Weight = 1.5
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                With .TextFrame.TextRange
                    .Text = Text
                    .Characters.Font.Bold = Textbold
                    .Characters.Font.Size = TextHeight
                    .Characters.Font.Name = L3DPPOFontFamily
                    .Characters.Font.Color.RGB = RGB(0, 0, 0)
                End With
            End With
        End Sub
    End Class
End Namespace